package Controller;

import Service.CourseContentService;
import domain.Course;
import domain.CourseSection;
import domain.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/courseContent")
public class CourseContentController {

    @Autowired
    private CourseContentService courseContentService;

    @RequestMapping("/findSectionAndLesson")
    public ResponseResult findSectionAndLessonByCourseID(int courseid){

        List<CourseSection> list = courseContentService.findSectionAndLessonByCourseID(courseid);

        ResponseResult responseResult = new ResponseResult(true,200,"响应成功",list);

        return responseResult;
    }


    @RequestMapping("/findCourseByCourseId")
    public ResponseResult findCourseById(int courseid){

        Course course = courseContentService.findCourseById(courseid);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("id",course.getId());
        map.put("courseName",course.getCourseName());

        ResponseResult responseResult = new ResponseResult(true,200,"响应成功",map);

        return responseResult;
    }


    /*
         新增 + 更新章节信息
     */
    @RequestMapping("/saveOrUpdateSection")
    public ResponseResult saveOrUpdateSection(@RequestBody CourseSection courseSection){

        ResponseResult result = null;

        // 判断是否携带了章节id

        // 更新操作
        if(courseSection.getId() != null) {

            courseContentService.updateSection(courseSection);
            result = new ResponseResult(true, 200, "更新章节成功", null);


        }else{
            // 新增操作
            courseContentService.saveOrUpdateSection(courseSection);
            result = new ResponseResult(true, 200, "响应成功", null);


        }
        return result;

    }


    /*
         修改章节状态
     */
    @RequestMapping("/updateSectionStatus")
    public ResponseResult updateSectionStatus(int id, int status){

        courseContentService.updateSectionStatus(id,status);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("status",status);

        ResponseResult responseResult = new ResponseResult(true,200,"状态修改成功",map);
        return responseResult;
    }







}
